package com.essc.serviceImpl;

import com.alibaba.druid.util.StringUtils;
import com.essc.mapper.GoodsMapper;
import com.essc.mapper.MessageMapper;
import com.essc.mapper.UserMapper;
import com.essc.pojo.Goods;
import com.essc.pojo.Message;
import com.essc.pojo.User;
import com.essc.service.MessageService;
import com.essc.utils.JsonResult;
import com.essc.utils.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//留言业务层
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    /*商品界面显示留言*/
    @Override
    public JsonResult findGoodMessage(Integer gid,Integer uid, Integer pageNum, Integer pageSize) {
        //设置分页 pageNum当前页 pageSize每页数量
        PageHelper.startPage(pageNum,pageSize);
        //获取留言内容
        List<Message> messageList = messageMapper.findMessageByGid(gid);
        //遍历
        for (int i=0;i<messageList.size();i++){
            Message message2 = new Message();
            Message message1i = messageList.get(i);
            //获取当前留言用户id
            Integer muid = message1i.getUserId();
            //获取当前留言用户个人信息
            User user= userMapper.findUserById(muid);
            //获取当前留言用户名 用户头像
            String username = user.getUsername();
            String avatar = user.getAvatar();
            boolean isShowDelBtn = false;
            message2.setShowDelBtn(false);
            /*判断该留言是否当前登录用户所留*/
            if(message1i.getUserId().equals(uid)){
                //配对上将可删isShowDelBtn变换为1
                message2.setShowDelBtn(true);
            }
            /*判断该商品是否当前用户所有*/
            //获取该卖家用户发布的所有商品信息
            List<Goods> goodsList = goodsMapper.findGoodsByUid(uid);
            String[] gids = new String[goodsList.size()];
            //把goodsList对象中的gid属性装进gids数组
            int count=0;
            for(Goods gd : goodsList){
                gids[count] = String.valueOf(gd.getGid());
                count++;
            }
            //比对gids[]中是否含有该商品
            for(String g : gids){
                if(g.equals(gid.toString())){
                    message2.setShowDelBtn(true);
                }
            }
            //获取留言信息重置
            message2.setMid(message1i.getMid());
            message2.setUserId(muid);
            message2.setGoodId(message1i.getGoodId());
            message2.setContent(message1i.getContent());
            message2.setCreateTime(message1i.getCreateTime());
            message2.setUsername(username);
            message2.setAvatar(avatar);
            messageList.set(i,message2);
        }
        int totalSize = (int) ((Page)messageList).getTotal();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,messageList);
        if(messageList.size()==0){
            return new JsonResult(201,"快来留言吧~");
        }
        return new JsonResult(200,"用户留言",pageResult);
    }

    /*个人页面显示留言*/
    @Override
    public JsonResult findMessageByUid(Integer uid) {
        List<Message> messageList = messageMapper.findMessageByUid(uid);
        for (int i=0;i<messageList.size();i++){
            Message message2 = new Message();
            Message message1i = messageList.get(i);
            //获取商品id
            Integer gid = message1i.getGoodId();
            //获取商品信息
            Goods good = goodsMapper.findGoodById(gid);
            //获取商品名
            String gname = good.getGname();
            //获取商品图片
            String pic = good.getPic();
            //获取留言信息重置
            message2.setMid(message1i.getMid());
            message2.setUserId(message1i.getUserId());
            message2.setGoodId(gid);
            message2.setContent(message1i.getContent());
            message2.setCreateTime(message1i.getCreateTime());
            message2.setGname(gname);
            message2.setPic(pic);
            messageList.set(i,message2);
        }
        if(messageList.size()==0){
            return new JsonResult(202,"快去给喜欢的商品留言吧~");
        }
        return new JsonResult(200,messageList);
    }

    /*新增留言*/
    @Override
    public JsonResult addMessage(Message message) {
        if(StringUtils.isEmpty(message.getContent())){
            return new JsonResult(201,"留言不能为空");
        }
        Object addMessage = messageMapper.addMessage(message);
        return new JsonResult(200,"留言成功",addMessage);
    }

    /*删除留言*/
    @Override
    public JsonResult deleteMessageByMid(Integer mid) {
        if(mid.equals("")||mid==null){
            return new JsonResult(201,"留言已被删除");
        }
        int delrow = messageMapper.deleteMessageByMid(mid);
        if(delrow!=1){
            return new JsonResult(202,"留言删除失败");
        }
        return new JsonResult(200,"删除成功",delrow);

    }


}
